<template>
  <el-form :model="form" label-width="130px" :rules="rules" ref="form" style="margin-top: -12px;">
    <el-form-item label="其他设置" class="one-label">
      <el-form-item label="商品重量：" label-width="170px">
        <el-input style="width: 200px" v-model="form.weight" class="long-input-2">
          <template slot="append">Kg</template>
        </el-input>
      </el-form-item>
      <el-form-item label="商品体积：" label-width="170px">
        <el-input style="width: 200px" v-model="form.size" class="long-input-2">
          <template slot="append">m³</template>
        </el-input>
      </el-form-item>
      <el-form-item label="商品产地：" label-width="170px" v-if="enterpriseConfig.crossBorderBusinessFlag === 1">
        <el-select v-model="form.countryId" filterable placeholder="请选择国家" @change="handleSelectCountry">
          <el-option v-for="item in countryData" :key="item.countryId" :label="item.countryName"
            :value="item.countryId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品地区：" label-width="170px">
        <el-row>
          <el-col :span="4">
            <div class="grid-content bg-purple">
              <el-input style="width: 200px" v-model="form.prov" class="long-input-2" placeholder="省份"
                :disabled="form.countryId !== '1447771317507129386' && enterpriseConfig.crossBorderBusinessFlag === 1 ? true :false" />
            </div>
          </el-col>
          <el-col :span="4">
            <div class="grid-content bg-purple-light">
              <el-input style="width: 200px" v-model="form.city" class="long-input-2" placeholder="城市"
                :disabled="form.countryId !== '1447771317507129386' && enterpriseConfig.crossBorderBusinessFlag === 1 ? true :false" />
            </div>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form-item>
  </el-form>
</template>


<script>
  import {
    getCountryList
  } from '@/api/system/country'
  import {
    getByEnterpriseId
  } from '@/api/user/enterprise'
  export default {
    directives: {},
    props: {},
    data() {
      return {
        form: {
          weight: '',
          size: '',
          prov: '',
          city: '',
          countryId: '1447771317507129386',
          countryName: ''
        },
        rules: {},
        // 国家列表
        countryData: [],
        // 系统参数
        enterpriseConfig: {
          // 增值功能跨境业务
          crossBorderBusinessFlag: 0
        }
      }
    },
    // 初始化页面
    created() {
      this.initEnterpriseConfig()
      this.initGoodsEdit()
      this.listCountryList()
    },
    mounted() {},
    methods: {
      initGoodsEdit() {
        const that = this
        this.$bus.$on('goods.edit.data', function(data) {
          that.form.weight = that.$utils.isExist(data.weight) ? data.weight : ''
          that.form.size = that.$utils.isExist(data.size) ? data.size : ''
          that.form.prov = that.$utils.isExist(data.prov) ? data.prov : ''
          that.form.city = that.$utils.isExist(data.city) ? data.city : ''
          that.form.countryId = that.$utils.isExist(data.countryId) ? data.countryId : '1447771317507129386'
          that.form.countryName = that.$utils.isExist(data.countryName) ? data.countryName : ''
          if (!that.form.countryName) {
            that.handleSelectCountry(that.form.countryId)
          }
        })
      },
      checkForm() {
        // 需要校验的集合
        const checkArrays = []
        let ret = true
        let message = ''
        this.$refs.form.validateField(checkArrays, (valid) => {
          if (valid !== '') {
            message = valid
            ret = false
            return
          }
        })
        if (!ret) {
          this.$message({
            type: 'error',
            message: message
          })
          return false
        }

        this.$refs['form'].validate((valid) => {
          if (valid) {
            ret = true
          }
        })
        return ret
      },
      listCountryList() {
        getCountryList().then(response => {
          if (response.isError) return
          this.countryData = response.data
          this.handleSelectCountry(this.form.countryId)
        })
      },
      handleSelectCountry(countryId) {
        if (countryId) {
          this.countryData.forEach((item) => {
            if (item.countryId === countryId) {
              this.form.countryName = item.countryName
              return
            }
          })
        }
      },
      // 获取系统参数
      initEnterpriseConfig() {
        getByEnterpriseId(this.getEnterpriseId()).then((response) => {
          if (response.isError) return
          this.enterpriseConfig = response.data
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  .span-bar {
    padding: 0 50px;
  }

  .remove-btn {
    padding: 7px 6px !important;
  }

  .long-input {
    width: 80%;
  }

  .long-input-2 {
    width: 55%;
    margin-right: 5%;
  }

  .long-input-3 {
    width: 55%;
  }

  .short-input {
    width: 20%;
  }

  .short-input-2 {
    width: 25%;
  }

  .default-input {
    width: 50%;
  }

  .tips {
    color: gray;
    font-size: 12px;
    line-height: 1.2;
    padding-top: 5px;
  }

  .picture {
    width: 130px;
    height: 130px;
    display: block;
  }

  .pic-bar {
    position: relative;

    .close-icon {
      position: absolute;
      right: -30px;
      top: -11px;
      width: 31px;
      height: 40px;
      padding: 0 7px;
      cursor: pointer;
    }

    span {
      position: absolute;
      right: 0;
      bottom: 0;
      width: 100%;
      display: block;
      color: #fff;
      text-align: center;
      padding: 3px 0;
      background: rgba(0, 0, 0, 0.5);
      cursor: pointer;
    }
  }

  .pic-add {
    background: #fff;
    border: #d3d3d3 1px dashed;
    border-radius: 5px;
    padding: 0 45px;
  }
</style>
